import psycopg2
import psycopg2.extras
import sys
from shapely.geometry import box
from shapely.wkt import dumps, loads
import gdal
from gdalconst import *
import os
import constant
import glob
import shutil

def re_griding(min_lon, min_lat, max_lon,max_lat,resx,resy,inputfile, ouputfile):
	regrid_command = "gdalwarp -s_srs '+proj=longlat +datum=WGS84' -t_srs '+proj=longlat +datum=WGS84' -ot Int16 -wt Int16 -te {0} {1} {2} {3} -tr {4} {5} -r cubic -overwrite {6} {7}".format(min_lon, min_lat, max_lon,max_lat,resx,resy,inputfile, ouputfile)
	#regrid_command = "gdalwarp -s_srs '+proj=longlat +datum=WGS84' -t_srs '+proj=longlat +datum=WGS84' -ot Int16 -wt Int16 -te 100.1 6.4 111.8 25.6 -tr {0} {1} -r cubic -overwrite {2} {3}".format(resx, resy, inputfile, ouputfile)
	
	os.system(regrid_command)
	
def re_griding2():
	#out12.tif
	
	min_lon_12 = 99.9998611
	min_lat_12 =  21.9998611
	max_lon_12 = 112.0001389
	max_lat_12 = 26.0001389
	
	#out34.tif
	min_lon_34 = 99.9998611
	min_lat_34 =  17.9998611
	max_lon_34 = 112.0001389
	max_lat_34 =  22.0001389
	
	
	#out56.tif
	min_lon_56 = 99.9998611
	min_lat_56 = 13.9998611
	max_lon_56 = 112.0001389
	max_lat_56 = 18.0001389

	#out789.tif
	min_lon_789 = 99.9998611
	min_lat_789 = 9.9998611
	max_lon_789 = 112.0001389
	max_lat_789 = 16.0001389
	
	resx=constant.res10x
	resy=constant.res10y

	inputfile_12="/home/phamha/Desktop/DEM/out12.tif"
	inputfile_34="/home/phamha/Desktop/DEM/out34.tif"
	inputfile_56="/home/phamha/Desktop/DEM/out56.tif"
	inputfile_789="/home/phamha/Desktop/DEM/out789.tif"
	
	outputfile_12_3km="/home/phamha/Desktop/OUT/out12_3km.tif"
	outputfile_12_6km="/home/phamha/Desktop/OUT/out12_6km.tif"
	outputfile_12_10km="/home/phamha/Desktop/OUT/out12_10km.tif"
	
	outputfile_34_3km="/home/phamha/Desktop/OUT/out34_3km.tif"
	outputfile_34_6km="/home/phamha/Desktop/OUT/out34_6km.tif"
	outputfile_34_10km="/home/phamha/Desktop/OUT/out34_10km.tif"
	
	outputfile_56_3km="/home/phamha/Desktop/OUT/out56_3km.tif"
	outputfile_56_6km="/home/phamha/Desktop/OUT/out56_6km.tif"
	outputfile_56_10km="/home/phamha/Desktop/OUT/out56_10km.tif"
	
	outputfile_789_3km="/home/phamha/Desktop/OUT/out789_3km.tif"
	outputfile_789_6km="/home/phamha/Desktop/OUT/out789_6km.tif"
	outputfile_789_10km="/home/phamha/Desktop/OUT/out789_10km.tif"
	
	#re_griding(min_lon_34, min_lat_34, max_lon_34, max_lat_34, constant.res6x, constant.res6x, inputfile_34, outputfile_34_6km)
	#re_griding(min_lon_34, min_lat_34, max_lon_34, max_lat_34, constant.res3x, constant.res3x, inputfile_34, outputfile_34_3km)
	
	re_griding(min_lon_56, min_lat_56, max_lon_56, max_lat_56, constant.res3x, constant.res3x, inputfile_56, outputfile_56_3km)
	re_griding(min_lon_56, min_lat_56, max_lon_56, max_lat_56, constant.res6x, constant.res6x, inputfile_56, outputfile_56_6km)
	re_griding(min_lon_56, min_lat_56, max_lon_56, max_lat_56, constant.res10x, constant.res10x, inputfile_56, outputfile_56_10km)
	
	re_griding(min_lon_789, min_lat_789, max_lon_789, max_lat_789, constant.res3x, constant.res3x, inputfile_789, outputfile_789_3km)
	re_griding(min_lon_789, min_lat_789, max_lon_789, max_lat_789, constant.res6x, constant.res6x, inputfile_789, outputfile_789_6km)
	re_griding(min_lon_789, min_lat_789, max_lon_789, max_lat_789, constant.res10x, constant.res10x, inputfile_789, outputfile_789_10km)
	
	
def mer_dem(inputfile, ouputfile, resx=0.117292465957718, resy=-0.117292465957718):
	con = None
	try:
		con = psycopg2.connect(host='192.168.3.4', database='fimo', user='rasdaman', password='rasdaman') 
		cur = con.cursor(cursor_factory=psycopg2.extras.DictCursor)
		cur.execute('SELECT filename, path, ST_AsText(corner) as corner FROM satorgdem limit 10')
		rows = cur.fetchall()
		
		merge_command = "python gdal_merge.py -o demorg.tif -ot Int16 -n 0 -a_nodata 0"
		for row in rows:
			filename = row["filename"].strip()
			filename=filename.replace(".tif","")
			
			path = row["path"].strip()
			path = os.path.join("/var/www/html/", path) #hardcode path to local folder path
			corner = loads(row["corner"])

			filepath = os.path.join(path, filename)
			merge_command += " " + filepath
		os.system(merge_command)

		#re_griding(inputfile, resx, resy, ouputfile)
		
	except psycopg2.DatabaseError, e:
		print 'Error %s' % e    
		sys.exit(1)
	finally:
		if con:
			con.close()	
			
def mer_dem2(inputfile, ouputfile, resx=0.117292465957718, resy=-0.117292465957718):
	merge_command = "python gdal_merge.py -o /home/phamha/Desktop/demorg.tif -ot Int16 -n 0 -a_nodata 0"
	for filename in glob.iglob("/home/phamha/Desktop/VNDEM/*"):
		merge_command += " " + filename
	os.system(merge_command)

		#re_griding(inputfile, resx, resy, ouputfile)
		
	
if __name__ == '__main__':
	# main(sys.argv[1], sys.argv[2], sys.argv[3],  sys.argv[4])
	outputfile_12_3km="/home/phamha/Desktop/OUT/out12_3km.tif"
	outputfile_12_6km="/home/phamha/Desktop/OUT/out12_6km.tif"
	outputfile_12_10km="/home/phamha/Desktop/OUT/out12_10km.tif"
	
	outputfile_34_3km="/home/phamha/Desktop/OUT/out34_3km.tif"
	outputfile_34_6km="/home/phamha/Desktop/OUT/out34_6km.tif"
	outputfile_34_10km="/home/phamha/Desktop/OUT/out34_10km.tif"
	
	outputfile_56_3km="/home/phamha/Desktop/OUT/out56_3km.tif"
	outputfile_56_6km="/home/phamha/Desktop/OUT/out56_6km.tif"
	outputfile_56_10km="/home/phamha/Desktop/OUT/out56_10km.tif"
	
	outputfile_789_3km="/home/phamha/Desktop/OUT/out789_3km.tif"
	outputfile_789_6km="/home/phamha/Desktop/OUT/out789_6km.tif"
	outputfile_789_10km="/home/phamha/Desktop/OUT/out789_10km.tif"
	"""
	merge_command = "python gdal_merge.py -o /home/phamha/Desktop/OUT/out_3km.tif -ot Int16 -n 0 -a_nodata 0"
	merge_command += " " + outputfile_12_3km
	merge_command += " " + outputfile_34_3km
	merge_command += " " + outputfile_56_3km
	merge_command += " " + outputfile_789_3km
	"""
	merge_command ="python gdal_merge.py -o /home/phamha/Desktop/OUT/out_3km.tif -ot Int16 -n 0 -a_nodata 0 /home/phamha/Desktop/OUT/out12_3km.tif /home/phamha/Desktop/OUT/out34_3km.tif /home/phamha/Desktop/OUT/out56_3km.tif /home/phamha/Desktop/OUT/out789_3km.tif"

	print merge_command
	os.system(merge_command)
	#mer_dem2(inputfile, outputfile, resx, resy)

	#shutil.copy(fullPath,"/home/phamha/Desktop/2011/")	
	#mer_dem(inputfile, outputfile, resx, resy)	
	
